<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.sz.ticket.dao.ITicketDAO" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String style1 = "width: 40%;text-align:right;font-size: 12px;";
String style2 = "text-align:left;width: 250px";

String style3 = "width: 150px;text-align:right;font-size: 12px;";

String userid = request.getSession().getAttribute("userid").toString();
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	    <link href="<%= request.getContextPath() %>/css/main.css" rel="stylesheet" type="text/css" />
		<base href="<%=basePath%>">

		<title>订单确认</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="myTicket">
		<meta http-equiv="description" content="我的订单">
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
    	</style>
	</head>
	
	<body onload="onLoad()">
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
		
		<table width="100%" height="50px" border="0" cellpadding="0" cellspacing="0">	  	 
	  	<tr>
		  	<td class="title0">订单确认</td>		  	
	  	</tr>	  	
	  	</table>
	  	
	  	<table width="100%" height="80px" border="0" cellpadding="0" cellspacing="0">
			<tr>
			  	<td style="<%=style3%>">商家编码&nbsp;</td>
			  	<td>
			  		<input  type="text" id="business_id" name="business_id"  style="<%=style2%>">
			  	</td>
			  	
			  	<td style="<%=style3%>">商家名称&nbsp;</td>
			  	<td>
			  		<input  type="text" id="business_name" name="business_name"  style="<%=style2%>">
			  	</td>				  	
		  	</tr>	

	  		<tr>
	  			<td style="<%=style3%>">客户电话&nbsp;</td>
			  	<td>
			  		<input  type="text" id="cust_tel" name="cust_tel"  style="<%=style2%>">
			  	</td>	
			  	<td style="<%=style3%>">客户姓名&nbsp;</td>
			  	<td>
			  		<input type="text" id="cust_name" name="cust_name" style="<%=style2%>">
			  	</td>			  	
		  	</tr>
		  	
		  	<tr>
		  	    <td style="<%=style3%>">预占状态&nbsp;</td>
		  	    <td style="font-size: 12px;">
		  	    	&nbsp;<input type="checkbox" id="lock_my" />我的预占
		  			<input type="checkbox" id="lock_null" />空闲		  			
		  	    </td>
		  	</tr>
		  </table>
		<table width="100%" height="200px" border="0" cellpadding="0" cellspacing="0">	
		 	<tr>
		  		<td colspan="2" class="title0">
            		<input type="button" id="btn" name="btn" value="查询" onclick="onClickQuery()">
	  			</td>
		  	
  			</tr>  
	  		<tr>
		  		<td colspan="2">
				  	<div id="table_div" style= "width: 100%"></div>
					<div id="pager"></div>
				</td>
			</tr>
	  	</table>
	  	<table width="100%" border="0" cellpadding="0" cellspacing="0" style="display: none">
	  		<tr>
	  			<td>
	  				<input type="button" id="btn_confirm" name="btn_confirm" value="确认订单" onclick="onConfirm()">
	  			</td>
	  		</tr>
	  	</table>	  	
	  	

		
		<script type="text/javascript">
		
		var numPerPage = 10; //每页行数
		var paramValue = ""; //形如  "{STATUS:'1', CREATE_MANAGER:'1'}"
		var methodCount = "queryTicketCount";
		var methodPage = "queryTicketPage";
		
		var table = new TableView('table_div');
		table.header = {
			TICKET_ID			: '订单编号',
			STATUS_NAME			: '订单状态',
			CUST_TEL			: '客户电话',
			CUST_NAME			: '客户姓名',
			BUSINESS_NAME		: '商家名称',			
			LOCK_MANAGER_NAME	: '预占人',
			CONFIRM_B_STR		: '商家确认',
			CONFIRM_C_STR		: '客户确认',
			DETAIL				: '详细信息',
			DO					: '解除锁定'		
		};
		table.customDefine = {
				TICKET_ID			: 'width = "100px"',
				STATUS_NAME			: 'width = "160px"',
				CUST_TEL			: 'width = "100px"',
				CUST_NAME			: 'width = "100px"',
				BUSINESS_NAME		: 'width = "160px"',
				LOCK_MANAGER_NAME	: 'width = "100px"',
				CONFIRM_B_STR		: 'width = "80px"',
				CONFIRM_C_STR		: 'width = "80px"',
				DETAIL				: 'width = "80px"',
				DO					: ''
		};
		
		table.dataKey = 'TICKET_ID';
		table.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.pager.size = numPerPage;

		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryTicket(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "TicketBMO.queryTicket",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});

  	  		/*
  			var ids = table.getSelectedKeys();
  			table.pager.size = 6;
  			
  			table.clear();
  			table.add({id: 2, name: 'kao2', name_cn: '杰瑞', text: 'Jerrimy'}); 
  			table.add({id: 3, name: 'kao3', name_cn: '杰瑞', text: 'Jerrimy'}); 
			var range = new Array(5);
			range.push({id: 40, name: 'kao', name_cn: '幽灵', text: 'None'});
			range.push({id: 60, name: 'kao', name_cn: '幽灵', text: 'None'});
			table.addRange(range);*/
  			return true;
  			
  		}
  		function onPagerClick(pageNO) {
  			queryTicket(pageNO, "FLASE");
  		}

  		//刷新当前页
  		function refreshCurrentPage() {
  			queryTicket(pager.index, "FLASE");
  		}
  		
  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);
		   for(var i = 0; i < array.length; ++i) {
		   		//形如：<a href="javascript:" onclick="about()">modal</a>
		   		var url = "<a href=\"javascript:\" onclick=\"onTicketDetail('";
		   		url += array[i].TICKET_ID;
		   		url += "')\">详细信息</a>";
		   		array[i].DETAIL = url;		   		
		   		
		   		url = "<input type=\"button\" value=\"释放\" onclick=\"onUnlockTicket('";
		   		url += array[i].TICKET_ID;
		   		url += "','";
		   		url += array[i].LOCK_MANAGER;
		   		url += "','";
		   		url += array[i].VERSION_STR;
		   		url += "')\" />";
		   		url += "<input type=\"button\" value=\"处理\" onclick=\"onConfirm('";
		   		url += array[i].TICKET_ID;
		   		url += "','";		   		
		   		url += array[i].VERSION_STR;
		   		url += "')\" />";
		   		array[i].DO = url;
		   		
		   		if(array[i].CONFIRM_B == "1") {
		   			array[i].CONFIRM_B_STR = "已确认";
		   		} else if(array[i].CONFIRM_B == "-1"){
		   			array[i].CONFIRM_B_STR = "确认失败";
		   		} else {
		   			array[i].CONFIRM_B_STR = "待确认";
		   		}
		   		if(array[i].CONFIRM_C == "1") {
		   			array[i].CONFIRM_C_STR = "已确认";
		   		} else if(array[i].CONFIRM_C == "-1"){
		   			array[i].CONFIRM_C_STR = "确认失败";
		   		} else {
		   			array[i].CONFIRM_C_STR = "待确认";
		   		}
		   }
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		}
  		function onClickQuery() {
  	  		
  			var tmp;
			paramValue = "{";			
  			
  			tmp = document.getElementById("cust_tel").value;
  			paramValue += " CUST_TEL:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("cust_name").value;
  			paramValue += " CUST_NAME:'" + tmp;
  			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_id").value;
  			paramValue += " BUSINESS_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_name").value;
  			paramValue += " BUSINESS_NAME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
  			
  			if(document.getElementById("lock_my").checked==true &&
  			document.getElementById("lock_null").checked==true) {
  				//我的预占和空闲都选了
  				paramValue += " __ADDITION:'(t.lock_manager="+<%=userid%>+" or t.lock_manager is null)',";
  				
  			} else if(document.getElementById("lock_my").checked==true) {
  			  paramValue += " LOCK_MANAGER:'" + <%=userid%>;
			  paramValue += "'";
			  paramValue += ",";  				
  			} else if(document.getElementById("lock_null").checked==true) {  			
  				paramValue += " __ADDITION:'t.lock_manager is null',";
  			} else {
  				//nothing
  			}
  			
  				

  			paramValue += " __ORDERBY:'order by t.version desc'";
  			  			
  			paramValue += "}";
  				
  	  		pager.index = 1;
  	  		queryTicket(1, true);
  		}
  		function onTicketDetail(ticket_id) {
  			var url = "<%=path%>";
  			url += "/page/viewTicket.jsp?ticket_id=" + ticket_id;
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:600px";
			//window.showModalDialog(url, window, param);
			
			var width = 1024; //弹出窗口的宽度;
			var height = 600; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"订单信息","height="+height+",width="+width+",top="+top+",left="+left+","+style);
			 			
  		}  		
  		
 		function showMsg(data) {
  	  		alert(data);
 		}
 		function lockSuccess(data) {
  	  		alert(data);
  	  		onClickQuery();  	  		
 		}
 		
 		function onUnlockTicket(ticket_id, lock_manager, myversion) {
 		
 			var str = "[";
			str += "{";
			str += "TICKET_ID:";
			str += "'" + ticket_id + "'";
			str += ",LOCK_MANAGER:";
			str += "'" + lock_manager + "'";
			str += ",MYVERSION:";
			str += "'" + myversion + "'";
			str += ",LOCKFLAG:";
			str += "'" + 0 + "'";
			str += "}"
  			str += "]";
  			
  			$.ajax({
				type: "POST",
				url: "ticket/lockTicket.ajax",
				//data: {id:'123'},
				data: {TICKETS:str, OPERATE_TYPE:"LOCK"},
				datatype:"json",
				success: lockSuccess,
				error: show_error
  	  		});
 		}
 		
 		function onConfirm(ticket_id, cur_version) {  			
  			url = "<%=path%>";
  			url += "/ticket/confirmTicket.do?ticket_id=" + ticket_id;
  			url += "&title=" + encodeURI(encodeURI("确认订单"));
  			url += "&mode=mode_confirm";
  			url += "&cur_version="+cur_version;
  			url += "&final_callback=refreshCurrentPage()"; //按目前的逻辑，确认成功后会转到“操作成功”的界面，在“操作成功”的界面回调这个函数刷新
  			var param = "status:no;scroll:no;dialogWidth:1000px;dialogHeight:700px";
			//window.showModalDialog(url, window, param);
			//refreshCurrentPage();
			
			var width = 1000; //弹出窗口的宽度;
			var height = 700; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"确认订单","height="+height+",width="+width+",top="+top+",left="+left+","+style);
			
 		}
 		
 		function onLoad() {
 			document.getElementById("lock_my").checked = true;
 			onClickQuery();
 		}
 		
 		function onlockTicket(ticket_id, cur_version) {
  			//var ids = table.getSelectedKeys(); // Array[Key]
  			
			var str = "[";

			str += "{";
			str += "TICKET_ID:";
			str += "'" + ticket_id + "'";
			str += ",MYVERSION:";
			str += "'" + cur_version + "'";
			str += ",LOCKFLAG:";
			str += "'" + 1 + "'";
			str += "}"			
	
  			str += "]";
  			
  			$.ajax({
				type: "POST",
				url: "ticket/lockTicket.ajax",
				//data: {id:'123'},
				data: {TICKETS:str, OPERATE_TYPE:"LOCK"},
				datatype:"json",
				success: lockSuccess,
				error: show_error
  	  		});
  			
  		}  		

	</script>
	
	</body>
</html>
